import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    searchKey: '', // 搜索的关键词
    addressList: []
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/'
    }
  },
  mounted() {
    this.getAddressList()
  },
  methods: {
    // 修改地址
    alertAddress(aid) {
      location.href = `/web/addAddress.html?aid=${aid}`
    },
    // 设置默认地址
    async setDefault(aid) {
      console.log('设置默认地址:', aid);
      const res = await request.put(`/address/setDefaultAddress/${aid}`)
      if (res.code === 200) {
        this.getAddressList()
        this.$message.success(res.msg)
      }
    },
    // 根据地址id删除地址
    async deleteAddress(aid) {
      console.log('删除地址:', aid);
      const res = await request.delete(`/address/deleteAddress/${aid}`)
      if (res.code === 200) {
        this.getAddressList()
        this.$message.success(res.msg)
      }
    },
    // 获取地址列表
    async getAddressList() {
      const res = await request.get('/address/getAddressList')
      this.addressList = res.data
      console.log(res)
    },
    // 查找商品
    searchProduct() {
      console.log('查找商品:');
      location.href = `../web/search.html?searchKey=${this.searchKey}`
    },
    // 退出登录
    logout() {
      localStorage.removeItem('token')
      location.href = '/'
    },
  }
})